package str_util

import (
	"database/sql"
	"encoding/json"
	"strconv"
)

func NewString(str string) *string {
	return &str
}

func SqlStringOrEmpty(str sql.NullString) string {
	if str.Valid {
		return str.String
	} else {
		return ""
	}
}
func EmptyOrDefault(ori, def string) string {
	if ori == "" {
		return def
	} else {
		return ori
	}
}

func MustToString(str interface{}) string {
	value, ok := str.(string)
	if !ok {
		panic("convert interface{} to string err")
	}
	return value
}

func GetInt64(ori string) (int64, error) {
	return strconv.ParseInt(ori, 10, 64)
}
func GetFloat64(ori string) (float64, error) {
	return strconv.ParseFloat(ori, 64)
}

func ToJson(o interface{}) string {
	jsonBytes, err := json.Marshal(o)
	if err != nil {
		return ""
	} else {
		return string(jsonBytes)
	}
}
func ToJsonPretty(o interface{}) string {
	jsonBytes, err := json.MarshalIndent(o, "", "  ")
	if err != nil {
		return ""
	} else {
		return string(jsonBytes)
	}
}
